package com.zhongyu.controller;

import com.zhongyu.pojo.Org;
import com.zhongyu.service.OrgService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/cus/org")
public class CusController {
    @Resource
    private OrgService orgService;
    @GetMapping("/list/{pageNo}/{pageSize}")
    public ResultVo list(@PathVariable Integer pageNo,@PathVariable Integer pageSize,Org org){
        PageResult pageResult= orgService.searchAll(pageNo,pageSize,org);
        return  ResultVo.success(pageResult);
    }
    @GetMapping("/search")
    public ResultVo search(){
        List<Org> list=orgService.searchAll();
        return ResultVo.success(list);
    }
    @PostMapping("/add")
    public ResultVo add(@RequestBody Org org){
        try {
            orgService.add(org);
            return ResultVo.success("部门添加成功！");
        }catch (Exception ex){
            return ResultVo.failure("部门添加失败！",ex);
        }
    }
    @GetMapping("/del")
    public ResultVo del(Integer id) {

        try {
            orgService.removeId(id);
            return ResultVo.success("该部门删除成功!");
        } catch (ResultVo resultVo) {

            return ResultVo.failure(" 该部门下面有员工，不能删除") ;
        }




    }

    @PostMapping("/update")//post请求要加@RequestBody进行接收
    public ResultVo update(@RequestBody Org org) {
        try {
            orgService.update(org);
            return ResultVo.success("修改用户成功");
        } catch (Exception ex) {
            return ResultVo.failure("修改用户失败", ex);
        }

    }
}
